//
//  NativeViewController.m
//  CJModileAdDemo
//
//  Created by CJ on 2022/3/7.
//

#import "NativeViewController.h"
#import <CJMobileAd/CJMobileAd.h>
#import <AnyThinkNative/AnyThinkNative.h>

@interface NativeViewController ()<ATNativeADDelegate>

@property (nonatomic, strong) CJNativeAd *nativeAd;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray <UIView *>*nativeAdViews;

@property (nonatomic, strong) UIView *feedView;

@end

@implementation NativeViewController

@synthesize customView = _customView;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)dealloc
{
    NSLog(@"事件:%s", __FUNCTION__);
}

- (void)setupUI {
    self.title = @"Native";
    [self.view addSubview:self.customView];
    self.customView.frame = CGRectMake(0, 100, 200, 200);
    self.customView.center = self.view.center;

}

- (void)loadAdData {
    [[ATAdManager sharedManager] loadADWithPlacementID:@"b65eadc6c80986" extra:@{kATExtraInfoNativeAdSizeKey: [NSValue valueWithCGSize:CGSizeMake(300, 0)]} delegate:self];
}

- (void)handleEvent:(NSInteger)eve {
    if (eve == 0) {
        [self loadAdData];
        return;
    }
    if ([[ATAdManager sharedManager] nativeAdReadyForPlacementID:@"b65eadc6c80986"]) {
        ATNativeAdOffer *offer = [[ATAdManager sharedManager] getNativeAdOfferWithPlacementID:@"b65eadc6c80986"];
        ATNativeADConfiguration *config = [[ATNativeADConfiguration alloc] init];
        config.ADFrame = CGRectMake(0, 128, 256, 88);
        config.delegate = self;
        config.rootViewController = self;
        // 创建nativeADView
        ATNativeADView *nativeADView = [[ATNativeADView alloc] initWithConfiguration:config currentOffer:offer placementID:@"b65eadc6c80986"];
        // 渲染广告
        [offer rendererWithConfiguration:config selfRenderView:nil nativeADView:nativeADView];
        self.feedView = nativeADView;
        [self.view addSubview:nativeADView];
    } else {
        NSLog(@"广告未准备好");
    }
}

- (CustomShowView *)customView {
    if (!_customView) {
        _customView = [[CustomShowView alloc] init];
        __weak NativeViewController *weakSelf = self;
        _customView.event = ^(NSInteger eve) {
            [weakSelf handleEvent:eve];
        };
    }
    return _customView;
}

- (NSMutableArray<UIView *> *)nativeAdViews {
    if (!_nativeAdViews) {
        _nativeAdViews = [NSMutableArray new];
    }
    return _nativeAdViews;
}

#pragma mark delegate datasource

/// Callback when the successful loading of the ad
/// 广告位加载成功
- (void)didFinishLoadingADWithPlacementID:(NSString *)placementID {
    [self.customView canShowStatus:true errorMsg:@""];
    NSLog(@"事件:%s", __FUNCTION__);
}

/// Callback of ad loading failure
/// 广告位加载失败
- (void)didFailToLoadADWithPlacementID:(NSString*)placementID
                                 error:(NSError*)error {
    [self.customView canShowStatus:false errorMsg:@""];
    NSLog(@"事件:%s", __FUNCTION__);
}
                                 
/// Native ads displayed successfully
/// 原生广告位展示成功
- (void)didShowNativeAdInAdView:(ATNativeADView *)adView placementID:(NSString *)placementID extra:(NSDictionary *)extra {
    NSLog(@"事件:%s", __FUNCTION__);
}

/// Native ad click
/// 原生广告位被点击
- (void)didClickNativeAdInAdView:(ATNativeADView *)adView
                     placementID:(NSString *)placementID
                           extra:(NSDictionary *)extra {
    NSLog(@"事件:%s", __FUNCTION__);
}

/// 关闭事件
- (void)didTapCloseButtonInAdView:(ATNativeADView *)adView placementID:(NSString *)placementID extra:(NSDictionary *)extra {
    NSLog(@"事件:%s", __FUNCTION__);
    [self.customView canShowStatus:false errorMsg:@""];
    [self.feedView removeFromSuperview];
}
@end
